<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>微信回调界面</title>
    <script type="application/javascript"  src="script/vue.js"></script>
    <script type="application/javascript"  src="script/axios.js"></script>
    <script type="application/javascript" src="script/common.js"></script>
</head>
<body>
<div id="callback">


</div>
<script type="text/javascript">
    new Vue({
        el:'#callback',
        data:{
            code:""
        },
        methods:{
            //拿到code后调用登录接口

            wxLogin(){
                debugger
                this.$http.post("/login/wechat/"+this.code)
                    .then(result=>{
                        debugger
                        result=result.data
                        if (result.success) { // 成功
                            //1. 登录成功直接返回主页，2此wx用户需要绑定跳转到绑定页面(还给传accessToken和openId）
                            if (result.resultObj.message && result.resultObj.message == "noBind") {
                                // 说明当前微信用户是第一次登录,需要跳转绑定页面
                                let {accessToken, openid} = result.resultObj;
                                location.href = "/binder.html?accessToken=" + accessToken + "&openid=" + openid;
                            } else {
                                //说明登录成功,拿到后端返回的token和loginUser,并存入localStrage中
                                alert("登录成功!");
                                let {token, loginUser} = result.resultObj;
                                localStorage.setItem("token", token)
                                localStorage.setItem("user", JSON.stringify(loginUser))
                                location.href = "/index.html";
                            }
                        }
                    })
            }

        },
        mounted(){
            // 页面一加载完毕,就要获取url中的code,并携带code调用后端接口
            let obj = url2Obj();
            this.code = obj.code;
            this.wxLogin();
        }

    })
</script>


</body>
</html>